<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
            <el-form-item :label="$t('vehicle.thref.vehicleCode')" prop="vehileCode">
                <el-input v-model="queryParams.vehicleCode"
                    :placeholder="$t('vehicle.thref.placeholder.vehicleCode')" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="IMEI" prop="imei">
                <el-input v-model="queryParams.imei" :placeholder="$t('vehicle.thref.placeholder.imei')" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item :label="$t('vehicle.thref.userName')" prop="userName">
                <el-input v-model="queryParams.userName" :placeholder="$t('vehicle.thref.placeholder.userName')"
                    clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item :label="$t('vehicle.thref.userPhone')" prop="userPhone">
                <el-input v-model="queryParams.userPhone"
                    :placeholder="$t('vehicle.thref.placeholder.userPhone')" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item :label="$t('vehicle.thref.threfTime')" prop="theftTime">
                <el-date-picker clearable v-model="queryParams.theftTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
                    :placeholder="$t('vehicle.thref.placeholder.threfTime')">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
                    $t('common.button.query') }}</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset')
                }}</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['vehicle:theft:add']">新增</el-button>
            </el-col> -->
            <!-- <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['vehicle:theft:edit']">修改</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['vehicle:theft:export']">{{ $t('common.button.export') }}</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="theftList" @selection-change="handleSelectionChange">
            <el-table-column label="ID" align="center" prop="id" />
            <!-- <el-table-column :label="$t('vehicle.thref.vehicleCode')" align="center" prop="vehicleCode" /> -->
            <el-table-column label="IMEI" align="center" prop="imei" />
            <el-table-column :label="$t('vehicle.thref.userName')" align="center" prop="userName" />
            <el-table-column :label="$t('vehicle.thref.plateNumber')" align="center" prop="number" />
            <el-table-column :label="$t('vehicle.thref.vehicleNick')" align="center" prop="nick" />
            <el-table-column :label="$t('vehicle.thref.userPhone')" align="center" prop="userPhone" />
            <el-table-column :label="$t('vehicle.thref.userEmail')" align="center" prop="userEmail" />
            <el-table-column :label="$t('vehicle.thref.status')" align="center" prop="theftStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.vehicle_theft_status" :value="scope.row.theftStatus" />
                </template>
            </el-table-column>
            <el-table-column :label="$t('vehicle.thref.threfTime')" align="center" prop="theftTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.theftTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="$t('common.table.operate')" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
                        v-hasPermi="['vehicle:theft:edit']">{{ $t('vehicle.thref.currentPosition') }}</el-button>
                        <el-button :disabled="scope.row.theftStatus != processed ?false:true"  size="mini" type="text" icon="el-icon-phone" @click="handleChange(scope.row)"
                            v-hasPermi="['vehicle:theft:remove']">{{ $t('vehicle.thref.handle') }}</el-button>
                 </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 当前失窃车辆失窃信息数据 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" label-position="left" :model="form" :rules="rules" label-width="170px">
                <el-form-item :label="$t('vehicle.thref.plateNumber')" prop="vehicleCode">
                    <el-input v-model="form.number" :disabled="true" />
                </el-form-item>
                <el-form-item label="IMEI" prop="imei">
                    <el-input v-model="form.imei" :disabled="true" />
                </el-form-item>
                <el-form-item :label="$t('vehicle.thref.threfTime')" prop="theftTime">
                    <el-date-picker clearable v-model="form.theftTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                        :disabled="true">
                    </el-date-picker>
                </el-form-item>
                <el-form-item :label="$t('vehicle.thref.longitude')" prop="longitude">
                    <el-input v-model="form.longitude" />
                </el-form-item>
                <el-form-item :label="$t('vehicle.thref.latitude')" prop="latitude">
                    <el-input v-model="form.latitude" />
                </el-form-item>
                <el-form-item :label="$t('vehicle.thref.address')" prop="lossAddress">
                    <el-input v-model="form.lossAddress" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="cancel">{{
                    $t('common.dialog.confirm') }}</el-button>
                <!-- <el-button @click="cancel">取 消</el-button> -->
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listTheft, getTheft, delTheft, addTheft, updateTheft,exportTheftList } from "@/api/vehicle/loss/data";
export default {
    name: "Theft",
    dicts: ["vehicle_theft_status"],
    data() {
        return {
            //未处理
            untreated:0,
            //已处理
            processed:1,
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 车辆失窃上报表格数据
            theftList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                vehileCode: undefined,
                imei: undefined,
                userName: undefined,
                userPhone: undefined,
                theftTime: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                id: [
                    { required: true, message: "ID不能为空", trigger: "blur" }
                ],
                vehileCode: [
                    { required: true, message: "车辆编码不能为空", trigger: "blur" }
                ],
                // imei: [
                //     { required: true, message: "关联IMEI不能为空", trigger: "blur" }
                // ],
                userName: [
                    { required: true, message: "用户姓名不能为空", trigger: "blur" }
                ],
                userPhone: [
                    { required: true, message: "用户电话不能为空", trigger: "blur" }
                ],
                // theftTime: [
                //     { required: true, message: "失窃时间不能为空", trigger: "blur" }
                // ],
                createTime: [
                    { required: true, message: "创建时间不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询车辆失窃上报列表 */
        getList() {
            this.loading = true;
            listTheft(this.queryParams).then(response => {
                this.theftList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                vehileCode: undefined,
                imei: undefined,
                userName: undefined,
                userPhone: undefined,
                theftTime: undefined,
                createTime: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加车辆失窃上报";
        },
        /** 修改按钮操作 */
        handleView(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getTheft(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = this.$t('vehicle.thref.vehicleAddress');
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.id != null) {
                        updateTheft(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addTheft(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 车辆失窃状态切换*/
        handleChange(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm(this.$t("vehicle.imgpairing.confirmProcess",{ids:ids})).then(() => {
                console.log(row);
            if (row.theftStatus != 0) {
                return;
            }
            row.theftStatus = 1;
            updateTheft(row).then(res =>{
                if (res.code != 200) {
                    this.$message.error(this.$t("common.operate.tip.processFail"))
                }else{
                    this.$message.success(this.$t("common.operate.tip.processSuccess"))
                    this.getList();
                }
            })
            })
           
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除车辆失窃上报编号为"' + ids + '"的数据项？').then(() => {
                this.loading = true;
                return delTheft(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('vehicle/theft/export', {
                ...this.queryParams
            }, `theft_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
